package com.xyd.servlet;

import com.xyd.entity.News;
import com.xyd.service.NewsService;
import com.xyd.service.impl.NewsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 86186
 */
@WebServlet("/NewsAddServlet")
public class NewsAddServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String title=request.getParameter("enTitle");
        String content=request.getParameter("enContent");

        News news=new News();
        NewsService newsService=new NewsServiceImpl();
        news.setEnTitle(title);
        news.setEnContent(content);
        Boolean flag=newsService.addNews(news);

        if (flag){
            request.getRequestDispatcher("NewsServlet?pageIndex=0").forward(request,response);
        }
        else {
            response.sendRedirect("newsAdd.jsp");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
